import sys
import os, os.path
import datetime
import time
import json
import shutil

class NDKHelper(object):
	def __init__(self,config):
		self._config = config;
		print self._config
	def set_ndk_path(self,ndk):
	    os.environ["PATH"] += os.pathsep + ndk
	def so_execute(self):
	    command = 'ndk-build ' \
	            +' NDK_PROJECT_PATH='+self._config['jni_path'] \
	            +' APP_BUILD_SCRIPT='+self._config['android_mk'] \
	            +' NDK_APP_APPLICATION_MK='+self._config['app_mk'] \
	            +' NDK_LIBS_OUT='+self._config['ndk_libs_out']
	    print 'start:    '+command
	    os.system(command)
	def move_so(self,so_src_path,so_dist_path):
	    print 'move so'
	    if os.path.exists(so_dist_path):
	        print ' dist exists '
	    else:
	        print ' dist not exists '
	    if os.path.exists(so_src_path):
	        for root, dirs, files in os.walk(so_dist_path, topdown=False):
	            for name in files:
	                os.remove(os.path.join(root, name))
	            for name in dirs:
	                os.rmdir(os.path.join(root, name))
	        for f in os.listdir(so_src_path):
	            print f
	            try:
	                shutil.move(os.path.join(so_src_path,f),so_dist_path)
	            except IOError,e:
	                print e
	    else:
	        print ' src not exists '
	    print so_src_path+so_dist_path

if __name__ == "__main__":
	NDK = 'D://Path//android//android-ndk-r12'
	WORK_PATH = os.getcwd()
	JNI_ROOT_PATH = os.path.dirname(os.getcwd())
	LIBS_PATH =os.path.join(JNI_ROOT_PATH,'libs') #os.path.join(os.path.dirname(os.path.dirname(JNI_ROOT_PATH)),'libs')
	print LIBS_PATH
	APP_MK = 'Application.mk'
	ANDROID_MK = 'Android.mk'
	NDK_LIBS_OUT = LIBS_PATH #os.path.join(WORK_PATH,'out')
	ndk_config = {}
	ndk_config['jni_path'] = JNI_ROOT_PATH
	ndk_config['android_mk'] = ANDROID_MK
	ndk_config['app_mk'] = APP_MK
	ndk_config['ndk_libs_out'] = NDK_LIBS_OUT
	ndkHelper = NDKHelper(ndk_config)
	ndkHelper.set_ndk_path(NDK)
	ndkHelper.so_execute()
	#ndkHelper.move_so(NDK_LIBS_OUT,LIBS_PATH)